package com.zqwh.xtool;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.hwit.HwitManager;
import com.zhy.autolayout.config.AutoLayoutConifg;
import com.zqwh.xtool.config.BaseConfig;
import com.zqwh.xtool.domain.UserVO;
import com.zqwh.xtool.model.CheckOrderModel;
import com.zqwh.xtool.model.DeviceModel;
import com.zqwh.xtool.model.HeartbeatModel;
import com.zqwh.xtool.model.LendOrderModel;
import com.zqwh.xtool.model.StockModel;
import com.zqwh.xtool.model.ToolModel;
import com.zqwh.xtool.model.UserModel;
import com.zqwh.xtool.model.VersionModel;
import com.zqwh.xtool.model.online.DeviceModelOnlineImpl;
import com.zqwh.xtool.model.online.HeartbeatModelOnlineImpl;
import com.zqwh.xtool.model.online.LendOrderModelOnlineImpl;
import com.zqwh.xtool.model.online.StockModelOnlineImpl;
import com.zqwh.xtool.model.online.UserModelOnlineImpl;
import com.zqwh.xtool.model.other.VersionModelImpl;
import com.zqwh.xtool.profile.BaseProfile;
import com.zqwh.xtool.receiver.ScreenStatusReceiver;
import com.zqwh.yj.sdk.model.RfidModel;
import com.zqwh.yj.sdk.model.RopenModel;
import com.zqwh.yj.sdk.model.RopenModelImpl;
import com.zqwh.yj.sdk.utils.LogUtils;
import com.zqwh.yj.sdk.utils.SerialPortUtils;
import com.zqwh.yj.sdk.utils.SharePreferencesUtils;
import com.zqwh.yj.sdk.utils.StringUtils;
import com.zqwh.yj.sdk.utils.httpRequest.RetrofitSingleton;


import static com.zqwh.xtool.config.BaseConfig.SHOW_NETWORK_LOG;

/**
 * Created by eason.yang on 2019/7/4.
 */
public class ToolsApp extends Application {
    public static UserModel gUserModel;
    public static DeviceModel gDeviceModel;
    public static ToolModel gToolModel;
    public static StockModel gStockModel;
    public static CheckOrderModel gCheckOrderModel;
    public static LendOrderModel gLendOrderModel;
    public static HeartbeatModel gHeartbeatModel;
    public static VersionModel gVersionModel;

    public static RfidModel gRfidModel;
    public static RopenModel gRopenModel;
    public static SerialPortUtils gNFCModel;

    public static UserVO gUser;
    public static Long gDevId;
    public static BaseProfile gProfile;
    public static String gDevNo = "000000";
    public static Context gContext;


    @Override
    public void onCreate() {
        super.onCreate();
        gContext = this;
        initUrl();

        initMode();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        ScreenStatusReceiver receiver = new ScreenStatusReceiver();
        registerReceiver(receiver, intentFilter);
        AutoLayoutConifg.getInstance().useDeviceSize();

    }

    private void initUrl() {
        String baseUrl = SharePreferencesUtils.readStringSharedPreferences(gContext, "BASE_URL");
        if (!StringUtils.isEmpty(baseUrl)){
            LogUtils.logout("Found local base url : "+baseUrl);
            BaseConfig.BASE_URL = baseUrl;
        }
        LogUtils.logout("Get base url : "+BaseConfig.BASE_URL);
        RetrofitSingleton.setShowNetworkDetail(SHOW_NETWORK_LOG);
    }

    private void initMode() {
        gHeartbeatModel = new HeartbeatModelOnlineImpl(this);
        gVersionModel = new VersionModelImpl(this);
        gDeviceModel = new DeviceModelOnlineImpl(this);
        gUserModel = new UserModelOnlineImpl(this);
        gLendOrderModel = new LendOrderModelOnlineImpl(this);
        gStockModel = new StockModelOnlineImpl(this);
        gRopenModel = new RopenModelImpl();
    }

    public static Long getDevId() {
        Long devId;
        if (gDevId == null){
            devId = SharePreferencesUtils.readLongSharedPreferences(gContext, "DEVICE_ID");
        }else {
            devId = gDevId;
        }
        return devId;
    }

    public static void setDevId(Long gDevId) {
        ToolsApp.gDevId = gDevId;
        SharePreferencesUtils.writeLongSharedPreferences(gContext, "DEVICE_ID", ToolsApp.gDevId);
    }
}
